package com.jdz.gxq.info.common.action;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.jdz.gxq.info.common.bo.SessionUserInfo;
import com.jdz.gxq.info.common.constant.Constant;

public abstract class CommonDispatchAction extends DispatchAction {

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		if (isCancelled(request)) {
			ActionForward af = cancelled(mapping, form, request, response);
			if (af != null) {
				return af;
			}
		}

		String name = getParameter(mapping, form, request, response);

		// String name = getMethodName(mapping, form, request, response, parameter);

		// Prevent recursive calls
		if ("execute".equals(name) || "perform".equals(name)) {
			String message = messages.getMessage("dispatch.recursive",
					mapping.getPath());

			log.error(message);
			throw new ServletException(message);
		}

		// Invoke the named method, and return the result
		return dispatchMethod(mapping, form, request, response, name);

	}

	protected String getUserId(HttpServletRequest request) {

		HttpSession session = request.getSession();

		SessionUserInfo user = (SessionUserInfo) session
				.getAttribute(Constant.SEESION_USER_KEY);

		return user.getUser().getName();
	}
}
